//
//  ConfigCellEnclosureView.m
//  JinganMindCloud
//
//  Created by JinganMac on 2020/7/3.
//  Copyright © 2020 JingAn. All rights reserved.
//

#import "ConfigCellEnclosureView.h"
#import "SubEnclosureCell.h"

@interface ConfigCellEnclosureView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UILabel  *leftTitleView;
@property (nonatomic, strong) UILabel  *rightContentView;
@property (nonatomic, strong) NSString *sourceUrl;
@property (nonatomic, strong) NSString *enclosureName;

@property (nonatomic, strong) UITableView *fileTableView;

@property (nonatomic, strong) NSArray *fileData;

@end

@implementation ConfigCellEnclosureView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addSubview:self.leftTitleView];
        [self addSubview:self.fileTableView];
    }
    return self;
}

-(void)layoutSubviews{
    [_leftTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self);
        make.width.mas_equalTo(self.leftViewWidth);
    }];
        
    [_fileTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(self);
        make.left.mas_equalTo(self.leftTitleView.mas_right);
    }];
    
}


- (UITableView *)fileTableView {
    
    if(!_fileTableView) {
        
        _fileTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _fileTableView.delegate =self;
        _fileTableView.dataSource =self;
        _fileTableView.estimatedRowHeight = 16.;
        _fileTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_fileTableView registerNib:[UINib nibWithNibName:@"SubEnclosureCell" bundle:[MCCToolClass MindCloudConfigCellBundle]] forCellReuseIdentifier:@"SubEnclosureCell"];
    }
    return _fileTableView;
}

-(void)setFilesDataSource:(NSArray *)array{
    self.fileData = array;
    [self.fileTableView reloadData];
    
    [_fileTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(self);
        make.height.mas_equalTo(16*self.fileData.count+8*(self.fileData.count-1));
        make.left.mas_equalTo(self.leftTitleView.mas_right);
    }];
}

#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.fileData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 16;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"SubEnclosureCell";
    SubEnclosureCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.filename.font = [UIFont systemFontOfSize:13.];
    cell.filename.textColor = [UIColor colorWithHexString:@"#326BD8"];
    NSDictionary *dict = self.fileData[indexPath.section];
    CCEnclosureFile *info = [CCEnclosureFile mj_objectWithKeyValues:dict];
    
    if ([self.viewConfiSets.cDefalutValue isEqualToString:@"fileNameDes"]) {
        cell.filename.text  = [NSString stringWithFormat:@"%@.%@",info.fileName,info.type];
    }else{
        cell.filename.text  = info.fileName;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.fileData[indexPath.section];
    CCEnclosureFile *info = [CCEnclosureFile mj_objectWithKeyValues:dict];
    [MGJRouter openURL:@"App_Common_WebKit" withUserInfo:@{@"baseUrl":info.webUrl,@"h5Type":@2,@"title":info.fileName,@"changeTitle":@"0",@"hideShareBtn":@(YES)} completion:nil];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (section==self.fileData.count-1) {
        return [UIView new];
    }else{
        UIView *footView = [UIView new];
        footView.backgroundColor = [UIColor clearColor];
        return footView;
    };
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section==self.fileData.count-1) {
        return 0.1;
    }else return 8.0;
}

-(UILabel *)leftTitleView{
    if (!_leftTitleView) {
        _leftTitleView = [UILabel new];
        _leftTitleView.textColor = [UIColor colorWithHexString:@"#AEAEAE"];
        _leftTitleView.font = [UIFont systemFontOfSize:13.];
        _leftTitleView.numberOfLines = 0;
    }
    return _leftTitleView;
}

-(UILabel *)rightContentView{
    if (!_rightContentView) {
        _rightContentView = [UILabel new];
        _rightContentView.textColor = [UIColor colorWithHexString:@"#326BD8"];
        _rightContentView.font = [UIFont systemFontOfSize:13.];
        _rightContentView.numberOfLines = 1;

    }
    return _rightContentView;
}

-(void)setViewConfiSets:(CellSubViewsConfigSetting *)viewConfiSets{
    self.leftTitleView.text = viewConfiSets.cTitleValue;
}

@end
